// pages/shop/detials/detials.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:{
      id: 1, 
      banner: [
        { img: '../../../asset/image/f1.jpg' },
        { img: '../../../asset/image/f2.jpg' },
        { img: '../../../asset/image/f3.jpg' },
        { img: '../../../asset/image/f4.jpg' }
      ],
      goodname:"phpshe测试商品韩版女夏装新款系带流苏拼接V领七分袖衬衫",
      detials:"fsdfsdfsfs",
      price:300.00,
      sell_num:300,
      freight:'包邮',
      spec:{
        size:[
          { id: 1, size: 'S' },
          { id: 2, size: 'M' },
          {id:3,size:'L'},
        ],
        color: [
          { id: 1, color: '红色' },
          { id: 2, color: '白色' },
          { id: 3, color: '蓝色' },
        ],
        stock:"300"
      }
    },
    gid:'',
    num:0,
    numC:1,
    size: '',
    sizeclass:'',
    colorclass:'',
    color:'',
    msg:"111111",
    cover:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      gid:options.gid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 获取组件
    this.dialog = this.selectComponent('#msg');
  },
  showDialog() {
    this.dialog.showDialog();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
   /**数量减 */
  dec:function(){
    var _data = this.data, num = parseInt(_data.numC) - 1;
    if(num>0){
      this.setData({
        numC: num,
      })
    }else{
      this.msgContent('不能再减啦')
    }
    console.log(_data.num)
  },
   /**加入购物车 */
  inca: function () {
    var _data = this.data, num = parseInt(_data.num)+1;
    this.setData({
      num: num
    })
  },
   /**加入购物车 */
  incb: function () {
    var _data = this.data, numC = parseInt(_data.numC) + 1;
    this.setData({
      numC: numC
    })
  },
   /**尺寸选择 */
  size:function(e){
    var sizeid = e.currentTarget.id, text = e.target.dataset.text;
    this.setData({
      sizeclass: sizeid,
      size:text
    })
  },
   /**颜色选择 */
  color: function (e) {
    var colorid = e.currentTarget.id,text = e.target.dataset.text;
    this.setData({
      colorclass: colorid,
      color: text
    })
  },
   /**商品规格选择 */
  show_hide:function(e){
    //获取组件
    var name = e.currentTarget.dataset.name,
        cover = this.data.cover,
        size = this.data.sizeclass,
        num = this.data.numC,
        color = this.data.colorclass,
        msg = "";
    if (name == 'car' && (!size || !color)){
      cover = !cover;
    } else if (name == 'car' && size && color){
      cover = cover;
      this.inca();
      msg = "已加入购物车";
      this.msgContent(msg)
    } else if (name == 'car-a' &&( !size || !color || !num)){
      cover = cover;
      msg = "请选择产品规格"
      this.msgContent(msg)
    } else if (name == 'car-a' && size && color && num) {
      cover = !cover;
      msg = "已加入购物车";
      this.sum(this.data.num,num)
      this.msgContent(msg)
    } else if (name != 'car-a' && name != 'car') {
      cover = !cover;
    }
    this.setData({
      cover:cover
    })
  },
   /**消息提示 */
  msgContent:function(val){
    this.setData({
      msg: val
    })
    this.showDialog();
  },
   /**两数之和 */
  sum:function(val1,val2){
    var s = parseInt(val1)+parseInt(val2)
    this.setData({
      num: s
    })
  },
  /**跳转到购物车 */
  toCar:function(){
    console.log(1)
    wx.switchTab({
      url: '/pages/shop/car/car',
      success:function(e){
        console.log(e)
      },
      fail:function(e){
        console.log(e)
      }
    })
  }
})